<template>
  <div class="qtUsers">
    <qtHeader fatherTitle="用户" fatherCreate="创建新的" @tell='getName'></qtHeader>
    <el-row class="qtContent">
      <el-col :span="24">
        <el-table :data="tableData" stripe style="width: 100%" height="778px" v-loading="loading">
          <el-table-column prop="loginName" label="登录名" width="120">
            <template slot-scope="scope">{{scope.row.names}}</template>
          </el-table-column>
          <el-table-column prop="quarters" label="岗位"></el-table-column>
          <el-table-column prop="username" label="用户名">
            <template slot-scope="scope">{{scope.row.nickname}}</template>
          </el-table-column>
          <el-table-column prop="phone" label="联系方式"></el-table-column>
          <el-table-column prop="departments" label="部门"></el-table-column>
          <!-- <el-table-column prop="assets" label="资产"></el-table-column>
          <el-table-column prop="hardware" label="硬件"></el-table-column>
          <el-table-column prop="consumables" label="耗材"></el-table-column>
          <el-table-column prop="license" label="许可证"></el-table-column> -->
          <el-table-column prop="status" label="状态">
            <!-- 切换状态需要使用template -->
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isActive"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="handleSelect(scope.row)"
                :disabled="scope.row.names == 'admin' ? true: false"
                :title="scope.row.names == 'admin' ? '管理员账号禁止修改状态': ''"
                >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button size="mini" type="warning" @click="handleEdit(scope.row)" :disabled="scope.row.names == 'admin' ? true: false"
              :title="scope.row.names == 'admin' ? '管理员账号禁止编辑': ''">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                :disabled="scope.row.names == 'admin' ? true: false"
                :title="scope.row.names == 'admin' ? '管理员账号禁止删除': ''"
              >
                <i class="el-icon-delete"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage1"
          :page-sizes="[15, 25, 35, 45]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>
    <!-- 添加弹框 -->
    <el-dialog  :visible.sync="dialogFormVisible" width="800px"  center custom-class="dialogClass" @close="cancel">
      <el-form :model="form" ref="dataform">
        <el-form-item label="公司" :label-width="formLabelWidth" prop="companyvalue">
          <el-select v-model="form.companyvalue" placeholder="请选择公司">
            <el-option  v-for="item in company" :key="item.id" :value="item.id" :label="item.companies"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth" prop="department">
          <el-select v-model="form.department" placeholder="请选择部门">
            <el-option  v-for="item in departments" :key="item.depid" :value="item.depid" :label="item.departments"></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="登录名" :label-width="formLabelWidth" prop="names">
          <el-input v-model="form.names" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="nickname">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="岗位" :label-width="formLabelWidth" prop="quarters">
          <el-input v-model="form.quarters" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserlist, userStatus, getCompanies, getDepartment, addUser, delUser, checkUser } from '@/api/index.js'
import qtHeader from '../components/header'
export default {
  data () {
    return {
      tableData: [],
      currentPage1: 1,
      total: 10,
      page: 1,
      pageSize: 15,
      disabled: false,
      loading: false,
      dialogFormVisible: false,
      formLabelWidth: '200px',
      form: {
        companyvalue: '',
        names: '',
        phone: '',
        quarters: '',
        password: '',
        nickname: '',
        department: ''
      },
      company: [],
      departments: []
    }
  },
  components: {
    qtHeader
  },
  created () {
    this.getUserlist()
  },
  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getUserlist()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getUserlist()
    },
    getUserlist () {
      this.loading = true
      getUserlist({ page: this.page, pageSize: this.pageSize }).then(res => {
        // console.log(res)
        if (res.data.msg === 'success') {
          this.tableData = res.data.data
          this.total = Number(res.data.total.total)
          this.loading = false
        }
      })
    },
    // 更改用户状态
    handleSelect (row) {
      userStatus({ id: row.id, isActive: row.isActive }).then(res => {
        if (res.data.code === 200) {
          this.$message.success('用户状态更新成功')
        } else {
          this.$message.error('用户状态更新失败')
        }
      })
    },
    // 显示弹窗
    getName (name) {
      this.dialogFormVisible = name
      // 弹框中的公司
      getCompanies().then(res => {
        // console.log(res)
        if (res.data.code === 200) {
          this.company = res.data.data
        }
      })
      getDepartment().then(res => {
        if (res.data.code === 200) {
          this.departments = res.data.data
        }
      })
    },
    confirm () {
      var obj = {
        companyvalue: this.form.companyvalue,
        names: this.form.names,
        phone: this.form.phone,
        quarters: this.form.quarters,
        password: this.form.password,
        nickname: this.form.nickname,
        department: this.form.department
      }
      if (this.assetsId) {
        obj.id = this.assetsId
        if (this.row.names !== this.form.names) {
          obj.names = this.form.names
        } else {
          obj.names = this.row.names
        }
        if (this.row.nickname !== this.form.nickname) {
          obj.nickname = this.form.nickname
        } else {
          obj.nickname = this.row.nickname
        }
        if (this.row.phone !== this.form.phone) {
          obj.phone = this.form.phone
        } else {
          obj.phone = this.row.phone
        }
        if (!isNaN(this.form.companyvalue)) {
          obj.companyvalue = this.form.companyvalue
        } else {
          obj.companyvalue = this.row.companiesId
        }
        if (!isNaN(this.form.department)) {
          obj.department = this.form.department
        } else {
          obj.department = this.row.departmentsId
        }
        checkUser(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success('修改成功')
            this.getUserlist()
          } else {
            this.$message.error('修改失败')
          }
        })
      } else {
        addUser(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success('用户创建成功')
            this.getUserlist()
          } else {
            this.$message.error('用户创建失败')
          }
        })
      }

      this.dialogFormVisible = false
    },
    cancel () {
      this.assetsId = ''
      this.$refs.dataform.resetFields()
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.dialogFormVisible = false
    },
    handleDelete (row) {
      this.$confirm('是否删除资产?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delUser(row.id).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
          // 判断当删除的数据为当前页中的最后一条数据，刷新页面的时候需要把当前页码数减1
          if (this.total / this.pageSize - parseInt(this.total / this.pageSize) <= 0.1) {
            this.page = this.page - 1
          }
          this.getUserlist()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    handleEdit (row) {
      this.getName(name)
      this.row = row
      // console.log(row)
      this.assetsId = row.id
      this.form.companyvalue = row.companies
      this.form.names = row.names
      this.form.phone = row.phone
      this.form.quarters = row.quarters
      // this.form.password,
      this.form.nickname = row.nickname
      this.form.department = row.departments
    }
  }
}
</script>

<style lang="scss" scoped>
.qtUsers {
  padding: 10px;
}

.qtImg {
  display: block;
  width: 75px;
  height: 100%;
}
.el-pagination {
  text-align: left;
  padding-top: 5px;
}
</style>
